package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.juntuo.entity.Company;
import com.juntuo.entity.Device;
import com.juntuo.entity.ParkConclude;
import com.juntuo.entity.User;
import com.juntuo.platformUtils.PostAsynCommandV4Util;
import com.juntuo.service.*;
import com.juntuo.shiro.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 简介：
 * 作者：Jason (chenxj@hadlinks.com)
 * 时间：2018/4/8 15:51
 */
@RestController
@RequestMapping("/api/test")
public class ApiTestOutController {
    org.apache.log4j.Logger log= org.apache.log4j.Logger.getLogger(ApiTestOutController.class);
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DeviceHistoryService deviceHistoryService;
    @Autowired
    private ParkConcludeService parkConcludeService;

//    @ModelAttribute
//    public void allowCrossOrigin(HttpServletResponse response){
//        response.setHeader("Access-Control-Allow-Origin","*");
//        response.setHeader("Access-Control-Allow-Methods","GET,POST");
//    }


    @RequestMapping(value="/noAuth",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getAllList(){
        Map<String,Object> res = new HashMap<>(1);
        res.put("code",0);
        return res;
    }


    @RequestMapping(value="/page",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<Map<String,Object>> getAllList(int pageNumber, int pageSize, String parkNumber, String name, int userId) throws Exception {
        User user = userService.selectByPrimaryKey(userId);
        String companyName=null;
        if(user.getCompanyId()!=null){
            Company company = new Company();
            company.setId(user.getCompanyId());
            company=companyService.selectOne(company);
            companyName=company.getName();
        }
        return deviceService.findPagedDevice(pageNumber,pageSize,null,null,null,companyName,parkNumber,name);
    }


    @RequestMapping(value="/park/concludes",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<Map<String,Object>> getParkConcludeList(int pageNumber, int pageSize,  int userId) throws Exception {
        User user = userService.selectByPrimaryKey(userId);
        return parkConcludeService.selectByCompanyId(pageNumber,pageSize,user.getCompanyId());
    }

//    @RequestMapping(value="/page",method = RequestMethod.GET)
//    @ResponseBody
//    public List<Map<String,Object>> getAllList(int pageNumber, int pageSize, String parkNumber, String name, int userId) throws Exception {
//        User user = userService.selectByPrimaryKey(userId);
//        String companyName=null;
//        if(user.getCompanyId()!=null){
//            Company company = new Company();
//            company.setId(user.getCompanyId());
//            company=companyService.selectOne(company);
//            companyName=company.getName();
//        }
//        return deviceService.findPagedDevice(pageNumber,pageSize,null,null,null,companyName,parkNumber,name);
//    }

}
